var io = require('socket.io-client');
//var robotInfo = require('./robot').getInstand;
var log = require("./../../util/loginfo").getInstand;
Holdem = function (_userInfo) {
    this._userId = 0;
    this._playCount = Math.floor(Math.random() * 2) + 1;
    this._online = false;
    this._hallSocket = null;
    this._gameSocket = null;
    this._account = null;
    this._sign = null;
    this._socre = 0;
    this._idx = 0;
    this._gameInfo = {};

    this.init = () => {
        this._idx = _userInfo.idx;
        this._account = _userInfo.account;
        //连接大厅
        this._hallSocket = io.connect('http://localhost:13000');
        this.addHallSocketListeners();
    };

    this.addHallSocketListeners = () => {
        this._hallSocket.on('connected', () => {
            this._hallSocket.emit('login', {
                userName: this._account,
                sign: '@@@@youmegame2019`@112233445566@@@@',
                gameId: 1
            })
        });

        this._hallSocket.on('loginResult', res => {
            // //log.info('^^^loginResult:'+JSON.stringify(res));
            if (res.resultid !== 1) {
                this.disconnect();
                return;
            }
            this._sign = res.Obj.sign;
            this._userId = res.Obj.id;
            this._socre = res.Obj.score;
            this._online = true;
        });

        this._hallSocket.on('ServerListResult', res => {
            log.info(this._userId + "进入德州扑克");
            let index = 0;
            for (let i = 0; i < res.GameInfo.length; i++) {
                if (res.GameInfo[i].GameId == 10) {
                    index = i;
                    break;
                }
            }
            this._gameSocket = io.connect('http://' + res.GameInfo[index].serverInfo.normal[this._idx].ip + ':' + res.GameInfo[index].serverInfo.normal[this._idx].prot);
            this.addHoldemSocketListeners();
        })
    };

    this.addHoldemSocketListeners = () => {
        this._gameSocket.on('connected', () => {
            this._gameSocket.emit("LoginGame", {userid: this._userId, gametype: 11, sign: this._sign});
        });

        this._gameSocket.on('loginGameResult', res => {
            // //log.info('^^^^进入德州游戏：'+JSON.stringify(res));
            if (res.resultid) {
                this._hallSocket.disconnect();
                this._gameInfo = {};
                this._gameInfo.playerCoin = res.Obj.score;
                this._gameSocket.emit('LoginRoom', {roomid: 1});
            }
        });

        this._gameSocket.on('LoginRoomResult', res => {
            // //log.info('^^^^进入德州房间：'+JSON.stringify(res));
            if (res.ResultCode) {
                this._gameInfo.tableId = res.ResultData.TableId;
                this._gameInfo.seatIndex = res.ResultData.seatId;
                this._gameInfo.tax = res.ResultData.tax;
                this._gameInfo.addScore = res.ResultData.addscore;
                this.ready();
                // //log.info('准备完成');
            }
        });

        this._gameSocket.on('game_myTurn_push', res => {
            setTimeout(() => {
                if (Math.random() * 100 <= 30) {
                    let offset = res.addMaxMoney - res.addMinMoney;
                    this._gameSocket.emit('jiazhu', res.addMinMoney);
                    return;
                }

                if (Math.random() * 100 <= 60 && res.canGen) {
                    this._gameSocket.emit('genpai');
                    return;
                }

                if (Math.random() * 100 <= 30 && res.canGuo) {
                    this._gameSocket.emit('rangpai');
                    return;
                }
                this._gameSocket.emit('qipai');
            }, Math.random() * 4000);
        });

        this._gameSocket.on('game_over', res => {
            this.ready();
        });

        this._gameSocket.on('exit_notify_push', res => {
            this._gameSocket.disconnect();
            this.init();
        });
    };

    this.ready = () => {
        this._gameSocket.emit('ready');
    };

    this.isOnline = () => {
        return this._playCount > 0;
    };

    this.disconnect = () => {
        if (this._hallSocket) {
            this._hallSocket.disconnect();
            this._hallSocket = null;
        }
        if (this._gameSocket) {
            this._gameSocket.disconnect();
            this._gameSocket = null;
        }
        this._online = false;
    };

    this.init();
};
module.exports = Holdem;